﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Sys.WebRes.Data;
using System.Web.Mvc;
using Sys.WebRes.Publish.Areas.Admin.Models;
using System.Web.Caching;

namespace Sys.WebRes.Publish.Areas.Admin.Controllers
{
    public class SelectItemFactory
    {
        /// <summary>
        /// 获取用户下拉框列表
        /// </summary>
        /// <returns></returns>
        public static List<SelectListItem> Users()
        {
            using (var db = new ResourceSysEntities())
            {
                var list = db.UserTBL.Where(p => p.Deleted == false)
                                        .OrderBy(p => p.RegisterDate)
                                        .Select(p => new { Value = p.ID, Text = p.Name }).ToList()
                                        .Select(p => new SelectListItem { 
                                            Value = p.Value.ToString(), 
                                            Text = p.Text })
                                        .ToList();

                list.Insert(0, new SelectListItem { Value = "0", Text = "--请选择--" });

                return list;
            }
        }

        /// <summary>
        /// 获取栏目下拉框列表
        /// </summary>
        /// <returns></returns>
        public static List<SelectListItem> Sections()
        {
            using (var db = new ResourceSysEntities())
            {
                var map = db.SectionTBL.Where(p => p.DeletedFlag == false)
                                        .OrderBy(p => p.OrderNo)
                                        .Select(p => new SectionModel{
                                            ID = p.ID,
                                            Name = p.Name,
                                            OrderNo = p.OrderNo,
                                            ParentID = (int?)p.Parent.ID,
                                            LevelNo = p.LevelNo})
                                        .ToDictionary(k => k.ID, v => v);


                var list = new List<SectionModel>();

                foreach (var item in map)
                {
                    if (item.Value.ParentID != null)
                    {
                        int key = item.Value.ParentID ?? 0;
                        map[key].children.Add(item.Value);
                        item.Value.LevelNo += 1;
                    }
                    else
                    {
                        list.Add(item.Value);
                    }
                }

                var result = new List<SectionModel>();
                result.Add(new SectionModel() { ID = 0, Name = "--请选择--" });
                SetToList(result, list, 0);

                return new SelectList(result, "id", "name").ToList();
            }
        }

        private static void SetToList(List<SectionModel> list, List<SectionModel> children, int levelNo)
        {
            children.ForEach(p => {
                //p.name = Math.Pow(10, levelNo).ToString().Replace('1', '├').Replace('0', '─') + p.name;
                
                list.Add(p);
                if (p.children != null && p.children.Count > 0)
                {
                    p.Name = "■".PadLeft(levelNo + 1, '　') + p.Name;
                    SetToList(list, p.children, levelNo + 1);
                }
                else
                {
                    p.Name = "●".PadLeft(levelNo + 1, '　') + p.Name;
                }
            });
        }
    }
}